package com.app.micaihu.view.user.userinfo.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.android.volley.VolleyError;
import com.app.config.LoadViewConfig;
import com.app.micaihu.R;
import com.app.micaihu.adapter.NewsAdapter;
import com.app.micaihu.app.AppApplication;
import com.app.micaihu.base.bean.DataBean;
import com.app.micaihu.bean.infor.UserHomePage;
import com.app.micaihu.bean.infor.UserHomePageNavBean;
import com.app.micaihu.bean.news.NewsEntity;
import com.app.micaihu.configure.TypeConstant;
import com.app.micaihu.configure.URIContant;
import com.app.micaihu.listener.HttpListener;
import com.app.micaihu.utils.HttpUtils;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


/**
 * Created by Guo on 2016/12/4.
 * 个人主页--新闻
 */

public class DynamicNewsFragment extends HeaderViewBaseFragment<NewsEntity> implements AdapterView.OnItemClickListener {



    private String toUid;//要查看的用户id

    private NewsAdapter mAdapter;
    protected static final String ARG_OBJECT = "object";
    protected static final String ARG_TOUID = "toUid";
    private UserHomePageNavBean navBean;



    public DynamicNewsFragment() {
    }


    public static DynamicNewsFragment newInstance(UserHomePageNavBean navBean, String toUid) {

        DynamicNewsFragment fragment = new DynamicNewsFragment();
        Bundle args = new Bundle();
        args.putString(ARG_TOUID, toUid);
        args.putSerializable(ARG_OBJECT, navBean);
        fragment.setArguments(args);

        return fragment;
    }




    public void initData( List<NewsEntity> list){
        if(list != null ){
            if(mDatalist == null) {
                mDatalist = new ArrayList<>();
            }
            mDatalist.addAll(list);
        }
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
//        LogUtils.e("gzq", "viewCreated" + "     listview = "+(mListView == null)+"    loadM "+(loadMoreView == null)+"    lvLoad = "+lvLoading);
        if(mListView == null || loadMoreView == null || lvLoading == null) return;

        mListView.setOnItemClickListener(this);

        navBean = (UserHomePageNavBean) getArguments().getSerializable(ARG_OBJECT);
        toUid = getArguments().getString(ARG_TOUID);



        if(mDatalist == null || mDatalist.size() <= 0){
            loadData(true);
        }else{
            if(mDatalist.size()>0 && mListView != null){
                if(mAdapter == null){
                    mAdapter =  new NewsAdapter(mDatalist, AppApplication.getInstance(), null);
                    try{
                        mListView.setAdapter(mAdapter);
                    }catch (NullPointerException e){
                        e.printStackTrace();
                    }
                }else{
                    mListView.setAdapter(mAdapter);
                    mAdapter.notifyDataSetChanged();
                }
                onComplete(1,null);
            }else{
                onComplete(3,AppApplication.getInstance().getString(R.string.my_home_empty_news));
            }

        }
    }





    /**
     * 加载数据
     *
     * @param isRefresh
     */
    @Override
    public void loadData(final boolean isRefresh) {
        if (navBean == null){
            if (mDatalist == null) {
                mDatalist = new ArrayList<>();
            }
            if (mAdapter == null) {
                mAdapter = new NewsAdapter(mDatalist, AppApplication.getInstance(), null);
                mListView.setAdapter(mAdapter);
            }

            onComplete(3, "暂无新闻");

            return;
        }

        HashMap<String, String> parms = new HashMap<>();
        parms.put("uid", toUid);
        parms.put("page", page + "");
        parms.put("type", navBean.getType());
        HttpListener<DataBean<UserHomePage>> httpListener = new HttpListener<DataBean<UserHomePage>>() {

            @Override
            public void onStart() {
                //设置正在刷新
                onRefreshing(null);
            }

            @Override
            public void onError(VolleyError error) {
                //设置刷新状态
                if(mDatalist == null || mDatalist.isEmpty()){
                    mListView.setEmptyView(lvLoading);
                    lvLoading.showErrorPage();
                    isRefreshing = false;
                }else{
                    onComplete(2, null);

                }
            }

            @Override
            public void onSuccess(DataBean<UserHomePage> response) {
                isRefreshing = false;
                if (response.noError() && response.getData() != null) {
                    List<NewsEntity> newsList = response.getData().getNewsList();
                    if (newsList.size() > 0) {
                        if (mDatalist == null) {
                            mDatalist = new ArrayList<>();
                        }
                        if (isRefresh) {
                            mDatalist.clear();
                        }
                        mDatalist.addAll(newsList);
                        if (mAdapter == null) {
                            mAdapter = new NewsAdapter(mDatalist, getActivity(),null);
                            mListView.setAdapter(mAdapter);
                        } else {
                            mAdapter.notifyDataSetChanged();
                        }

                        onComplete(1, "");//展示底部空view
                    } else {
                        if (mDatalist == null || mDatalist.isEmpty()) {//展示空view
                            mListView.setEmptyView(lvLoading);
                            lvLoading.showEmptyPage(LoadViewConfig.EMPTY_DEFAULT,AppApplication.getInstance().getString(R.string.my_home_empty_news));
                            isRefreshing = false;

                        } else {
                            onComplete(2, AppApplication.getInstance().getString(R.string.pullup_nomore_data));//展示底部空view
                        }
                    }
                } else {
                    if (mDatalist == null || mDatalist.isEmpty()) {//展示errorPage
                        mListView.setEmptyView(lvLoading);
                        lvLoading.showErrorPage();
                        isRefreshing = false;
                    } else {
                        onComplete(2,AppApplication.getInstance().getString(R.string.pullup_nomore_data));
                    }
                }


            }
        };

        HttpUtils.addPostRequest(URIContant.MY_HOMEN_AUTHOR_PAGE, new TypeToken<DataBean<UserHomePage>>() {
        }.getType(), "", parms, httpListener);
    }



    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
        //因为有头部

        if (mListView.getHeaderViewsCount() > 0) {
            position = position - mListView.getHeaderViewsCount();
        }
        //防止越界
        if (position >= mDatalist.size()) {
            position = mDatalist.size() - 1;
        } else if (position < 0) {
//            position = 0;
            return;
        }
        NewsEntity news = mDatalist.get(position);
        if (!TypeConstant.openTypeDetail(news, getActivity(), TypeConstant.SOURCETYPE_NORMAL)) {

            if (news == null) {
                return;
            }

        }
        //添加已读文章id
        TextView textView = (TextView) view.findViewById(R.id.item_public_title);
        if (textView != null) {
            textView.setEnabled(false);
        }
        //添加已读新闻
        NewsAdapter.addReadList(news.getArticleId());


    }
}
